package com.jackstraw.jack_base.datasource.jpa.base.service;

import com.jackstraw.jack_base.datasource.database.parten.base.service.CrudService;
import com.jackstraw.jack_base.datasource.jpa.base.dao.BaseDao;
import com.jackstraw.jack_base.datasource.jpa.base.dao.database.JpaDao;
import com.jackstraw.jack_base.datasource.jpa.base.entity.Query;
import com.jackstraw.jack_base.datasource.jpa.base.entity.po.JpaPo;
import com.jackstraw.jack_base.util.CollectionUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public interface JpaService<DAO extends JpaDao<PO, ID>,
        PO extends JpaPo<PO, ID>, BO extends PO, ID>
        extends CrudService<DAO, PO, BO, ID> {


    /**
     * @return
     */
    BaseDao getBaseDao();

    /**
     * 通过params创建查询分页条件
     *
     * @param params
     * @return
     */
    Query<PO> query(Map<String, Object> params);

    /**
     * 通过params创建查询条件
     *
     * @param params
     * @param query
     * @return
     */
    Query<PO> criteriaQuery(Map<String, Object> params, Query<PO> query);

    /**
     * 主键字段名称
     *
     * @return
     */
    default String idFieldName(){
        return "";
    }

    @Override
    default List<ID> poToKeyList(List<PO> pos) {
        if (CollectionUtil.isEmpty(pos)) {
            return new ArrayList<>();
        }
        return pos.stream().map(po -> po.keyValue()).collect(Collectors.toList());
    }

    @Override
    default List<ID> boToKeyList(List<BO> bos) {
        if (CollectionUtil.isEmpty(bos)) {
            return new ArrayList<>();
        }
        return bos.stream().map(bo -> bo.keyValue()).collect(Collectors.toList());
    }
}
